#pragma once
#include <afxwin.h>
#include "CBaseException.h"
#include "CSQLite/CSQLite.h"

class CBaseEntity {
	protected:
		BOOL		m_ThrowExceptions;		// Do u like catching errors?
		CSQLite*	m_pDB;					// Useful thing

	public:
		/**
		 * Constructors, Destructors & other stuff
		 */
		CBaseEntity( void ) {
			this->m_ThrowExceptions = 0;
			this->m_pDB = NULL;
		}

		~CBaseEntity( void ) {
			#ifdef _DEBUG
				TRACE( "[CBaseEntity]: Destroying...\n" );
			#endif
		}

		/**
		 * DB Handlers
		 */
		void SetDB( CSQLite* pDB ) {
			this->m_pDB = pDB;
		}

		CSQLite* GetDB( void ) const {
			return this->m_pDB;
		}

		virtual void OnError( void ) {
			return;
		}

	protected:
		virtual void __Error( int error_id, const char* error_text = "N/A" ) {
			#ifdef _DEBUG
				TRACE( "[CBaseEntity]: Error! ( ID=%d, MSG=%s )\n", error_id, error_text );
				::AfxMessageBox( CString(error_text), MB_ICONERROR );
			#endif
			if( this->m_ThrowExceptions ) {
				throw CBaseException( error_id, error_text );
			}
			this->OnError();
		}

		CTime __CStringToCTime( LPCTSTR Time ) {
			int d, m, y, h, i, s;
			_stscanf( Time, _T("%4d-%2d-%2d %2d:%2d:%2d"), &y, &m, &d, &h, &i, &s );
			if( y < 1900 || d > 31 || m > 12 || h > 23 || i > 59 || s > 59 ) {
				this->__Error( 500, "Time reading error" );
				return CTime();
			}
			return CTime( y, m, d, h, i, s );
		}
};